package com.yoo.ons.listener

import android.content.Context
import android.view.GestureDetector
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView

/**
 * 监听recyclerview的滑动事件，以此来切换banner
 */
class OnSwipeTouchListener(
    context: Context,
    private val recyclerView: RecyclerView,
    private val onSwipeCallback: (RecyclerView.ViewHolder, SwipeDirection) -> Unit
) : RecyclerView.OnItemTouchListener {

    private val gestureDetector: GestureDetector

    init {
        gestureDetector = GestureDetector(context, GestureListener())
    }

    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
        gestureDetector.onTouchEvent(e)
        return false
    }

    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}

    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}

    private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {

        private val SWIPE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100

        override fun onDown(e: MotionEvent): Boolean {
            return true
        }

        override fun onFling(
            e1: MotionEvent?,
            e2: MotionEvent,
            velocityX: Float,
            velocityY: Float
        ): Boolean {
            val x = e1?.x ?: 0f
            val y = e1?.y ?: 0f
            val diffX = e2.x - x
            val diffY = e2.y - y
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        e1?.let {
                            getCurrentViewHolder(it)?.let {
                                onSwipeCallback(
                                    it,
                                    SwipeDirection.RIGHT
                                )
                            }
                        }
                    } else {
                        e1?.let {
                            getCurrentViewHolder(it)?.let {
                                onSwipeCallback(
                                    it,
                                    SwipeDirection.LEFT
                                )
                            }
                        }
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        e1?.let {
                            getCurrentViewHolder(it)?.let {
                                onSwipeCallback(
                                    it,
                                    SwipeDirection.DOWN
                                )
                            }
                        }
                    } else {
                        e1?.let {
                            getCurrentViewHolder(it)?.let {
                                onSwipeCallback(
                                    it,
                                    SwipeDirection.UP
                                )
                            }
                        }
                    }
                }
            }
            return true
        }

        private fun getCurrentViewHolder(e: MotionEvent): RecyclerView.ViewHolder? {
            val childView = recyclerView.findChildViewUnder(e.x, e.y)
            return childView?.let { recyclerView.getChildViewHolder(it) }
        }
    }

    enum class SwipeDirection {
        LEFT, RIGHT, UP, DOWN
    }
}